package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {


    @Autowired
    private CourseBaseService courseBaseService;


    /*
     * SpringMVC 的传入参数默认使用的是：QueryString
     *       如果参数的key值和封装类型的属性名一致，@QueryParam注解可以省略掉
     *
     * */
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        PageVO pageVO = courseBaseService.queryCourseList(params, model, companyId);
        return pageVO;
    }

    /**
     * 修改课程状态为提交状态
     * @param vo
     * @return
     */
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.获得用户登录的标识信息（companyid）
        Long companyId = SecurityUtil.getCompanyId();

        // 2.将vo转为dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(vo);

        courseBaseDTO.setCompanyId(companyId);


        // 3.调用service获得结果并返回
        CourseBaseDTO resultDTO = courseBaseService.createCourseBase(courseBaseDTO);

        return resultDTO;
    }


    /**
     * 数据回显
     *
     * @param courseBaseId
     * @return
     */
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.getByCourseId(courseBaseId, companyId);
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        // 1.获得用户登录的标识信息（companyid）
        Long companyId = SecurityUtil.getCompanyId();

        // 2.将vo转为dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        courseBaseDTO.setCompanyId(companyId);


        // 3.调用service获得结果并返回
        CourseBaseDTO resultDTO = courseBaseService.modifyCourseBase(courseBaseDTO);

        return resultDTO;
    }

    /**
     * 删除课程
     *
     * @param courseBaseId
     */
    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        //获取教育机构id
        Long companyId = SecurityUtil.getCompanyId();

        //调用删除方法
        courseBaseService.removeCourseBase(courseBaseId, companyId);

    }

    /**
     * 课程提交
     *
     * @param courseBaseId
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        //获取教育机构id
        Long companyId = SecurityUtil.getCompanyId();
        //调用方法
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }
}
